package session

import (
	"context"

	"gitee.com/mkwchecking/sr-core/yal/ymap"
	"gitee.com/mkwchecking/sr-core/yerr"
)

type StorageFac func(ctx context.Context, cfg SessionConfig) (Storage, error)

var _storeRegister = ymap.New[SessionStoreType, StorageFac]()

func Register(storeType SessionStoreType, fac StorageFac) {
	_storeRegister.Register(storeType, fac)
}

func Unregister(storeType SessionStoreType) {
	_storeRegister.Unregister(storeType)
}

// 构造
func NewStore(ctx context.Context, cfg SessionConfig) (Storage, error) {
	fac, ok := _storeRegister.Get(cfg.StoreType)
	if !ok {
		return nil, yerr.ErrBizParamInvalid.Error()
	}

	return fac(ctx, cfg)
}
